<?php
FLEA::loadClass('FLEA_Db_TableDataGateway');

class	Model_TradeMark	extends		FLEA_Db_TableDataGateway
{
	var		$tableName		=	'trademark';
	var		$primaryKey		=	'TradeMarkID';

	function findTradeMarks($clientID)
	{
		$sql	=	"
			select client.Business , trademark.* 
			from client , trademark
			where trademark.ClientID = client.ClientID
		";
		if($clientID > 0)
		{
			$sql	.=	' and client.ClientID='.$clientID;
		}

		return $this->findBySql($sql);
	}

	function findAllTradeMarks($userID,$role)
	{
		$sql	=	"
			select distinct trademark.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName,bid.Budget
				
				 from 
			
				trademark
							
				left join client on trademark.ClientID=client.ClientID 
							
				left join firm on trademark.FirmID=firm.FirmID
							
				left join inventor on trademark.InventorID=inventor.InventorID
							
				left join practitioner on trademark.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
		";

		switch($role)
		{
			case 1:
				$sql	.=	' left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
						where trademark.ClientID='.$userID;
				break;
			case 2:
				$sql	.=	' left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
						where trademark.InventorID='.$userID;
				break;
			case 3:
				$sql	.=	" left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
					where trademark.FirmID=".$userID;
				break;
			case 4:
				$sql	.=	' left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
				 where trademark.PractitionerID='.$userID;
				break;
		}
		
		return $this->findBySql($sql);
	}
	
	function findBidTradeMarks($firmID)
	{
		$sql	=	"
			select distinct trademark.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName,bid.Budget
				
				 from 
			
				trademark
							
				left join client on trademark.ClientID=client.ClientID 
							
				left join firm on trademark.FirmID=firm.FirmID
							
				left join inventor on trademark.InventorID=inventor.InventorID
							
				left join practitioner on trademark.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
		";
		
		$sql	.=	" left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$firmID."
					where trademark.BBid=1 and trademark.FirmID=0 ";
		
		return $this->findBySql($sql);
	}
	
	function findTradeMark($tradeMarkID,$userID,$role)
	{
		$sql	=	"
			select distinct trademark.*,client.Business as ClientName,firm.Name as FirmName,
				inventorUser.Email as InventorEmail,inventorUser.FirstName as InventorFirstName,inventorUser.LastName as InventorLastName,
				practitionerUser.Email as PractitionerEmail,practitionerUser.FirstName as PractititonerFirstName,
				practitionerUser.LastName as PractitionerLastName,bid.Budget
				
				 from 
			
				trademark
							
				left join client on trademark.ClientID=client.ClientID 
							
				left join firm on trademark.FirmID=firm.FirmID
							
				left join inventor on trademark.InventorID=inventor.InventorID
							
				left join practitioner on trademark.PractitionerID=practitioner.PractitionerID
				
				left join user as inventorUser on inventorUser.UserID=inventor.UserID
				
				left join user as practitionerUser on practitionerUser.UserID=practitioner.UserID
				
				
		";
		
		if($role == 3)
		{
			$sql	.=	"left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
					where ";
			$sql	.=	"  trademark.TradeMarkID=".$tradeMarkID;
		}
		else
		{
			$sql	.=	"left join bid  on trademark.TradeMarkID=bid.ID and bid.Type=1 and bid.FirmID=".$userID."
					";
			$sql	.=	" where trademark.TradeMarkID=".$tradeMarkID;
		}
		
		$marks	=	$this->findBySql($sql);
		return $marks[0];
	}
}
?>